package com.gk.study.controller;

import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.entity.Order;
import com.gk.study.entity.Thing;
import com.gk.study.permission.Access;
import com.gk.study.permission.AccessLevel;
import com.gk.study.service.OrderService;
import com.gk.study.service.ThingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    private final static Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    OrderService service;

    @Autowired
    ThingService thingService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public APIResponse list() {
        List<Order> list = service.getOrderList();

        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }

    // 用户订单
    @RequestMapping(value = "/userOrderList", method = RequestMethod.GET)
    public APIResponse userOrderList(String userId, String status) {
        List<Order> list = service.getUserOrderList(userId, status);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }


    // 订单创建
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Transactional
    //添加多线程锁
    public APIResponse create(Order order) throws IOException {
//        service.createOrder(order);
//
//        order = service.getOrderById(order.getId());
//        Long thingId = order.getThingId();
//        Thing thing = new Thing();
//        thing.setId(thingId);
//        thing.setCheckIn(1);
//        thingService.updateThing(thing);

        final Object lock = new Object();

        synchronized (lock) {
            service.createOrder(order);

            order = service.getOrderById(order.getId());
            Long thingId = order.getThingId();
            Thing thing = new Thing();
            thing.setId(thingId);
            thing.setCheckIn(1);
            thingService.updateThing(thing);
        }


        return new APIResponse(ResponeCode.SUCCESS, "创建成功");

    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public APIResponse delete(String ids) {
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteOrder(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Transactional
    public APIResponse update(Order order) throws IOException {
        service.updateOrder(order);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @Transactional
    public APIResponse cancelOrder(Long id) throws IOException {
        Order order = new Order();
        order.setId(id);
        order.setStatus("7"); // 7=取消
        order.setCheckIn(0);
        service.updateOrder(order);

        order = service.getOrderById(id);
        Long thingId = order.getThingId();
        Thing thing = new Thing();
        thing.setId(thingId);
        thing.setCheckIn(0);
        thingService.updateThing(thing);



        return new APIResponse(ResponeCode.SUCCESS, "取消成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/cancelUserOrder", method = RequestMethod.POST)
    @Transactional
    public APIResponse cancelUserOrder(Long id) throws IOException {
//        Order order = new Order();
//        order.setId(id);
//        order.setStatus("7"); // 7=取消
//        order.setCheckIn(0);
//        service.updateOrder(order);
//
//        order = service.getOrderById(id);
//        Long thingId = order.getThingId();
//        Thing thing = new Thing();
//        thing.setId(thingId);
//        thing.setCheckIn(0);
//        thingService.updateThing(thing);

        Order order = new Order();
        order.setId(id);
        order.setStatus("6"); // 申请取消
//        order.setCheckIn(0); //0 未入住
        service.updateOrder(order);


        return new APIResponse(ResponeCode.SUCCESS, "取消成功");
    }


    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/checkUserOrder", method = RequestMethod.POST)
    @Transactional
    public APIResponse checkUserOrder(Long id) throws IOException {
        Order order = new Order();
        order.setId(id);
        order.setCheckIn(2);
        service.updateOrder(order);


        order = service.getOrderById(id);
        Long thingId = order.getThingId();
        Thing thing = new Thing();
        thing.setId(thingId);
        thing.setCheckIn(2);
        thingService.updateThing(thing);

        return new APIResponse(ResponeCode.SUCCESS, "入住成功");
    }


    @Access(level = AccessLevel.ADMIN)
    @RequestMapping(value = "/outUserOrder", method = RequestMethod.POST)
    @Transactional
    public APIResponse outUserOrder(Long id) throws IOException {
        Order order = new Order();
        order.setId(id);
        order.setCheckIn(0);
        order.setStatus("8"); //已完成订单
        service.updateOrder(order);


        order = service.getOrderById(id);
        Long thingId = order.getThingId();
        Thing thing = new Thing();
        thing.setId(thingId);
        thing.setCheckIn(0);
        thingService.updateThing(thing);

        return new APIResponse(ResponeCode.SUCCESS, "退房成功");
    }

}
